﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BPM.Web;
using BPM.Service;
using BPM.Common;
using BPM.ViewModel;
using BPM.Web;
using System.Dynamic;
using BPM.Entity;

namespace BPM.Controllers
{
	public class AuthorityController : InternalViewController
	{
		private UserService _uService;

		public AuthorityController()
		{
			_uService = new UserService();
		}

		/// <summary>
		/// 系统中用户的列表
		/// </summary>
		/// <returns></returns>
		public ActionResult UserList(int? page)
		{
			var users = _uService.GetAllUser_WithRoles(page ?? 1, StaticData.PagingCount);
			this.ViewData["Users"] = users;
			return View();
		}

		/// <summary>
		/// 修改某人的authority
		/// </summary>
		/// <returns></returns>
		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult AssignAuthority(int userId)
		{
			var user = _uService.GetById(userId);

			return View(user);
		}

		public ActionResult AJAX_SubmitRoles(int userId, string roles)
		{
			var res = new UserService().ResetRoles(userId, roles, this.CurrentUser);
			return Json(res);
		}

		/// <summary>
		/// 库管员是否可以查看进货单
		/// </summary>
		/// <returns></returns>
		public ActionResult StoragerWhetherViewPurchaseOrder()
		{
			return View();
		}

		/// <summary>
		/// 等待审核员工列表
		/// </summary>
		/// <returns></returns>
		public ViewResult WaitForValidUserList(int? page)
		{
			var users = _uService.GetNotValidUserList(page ?? 1, StaticData.PagingCount);
			return View(users);
		}

		/// <summary>
		/// 删除一个验证过的用户
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public ActionResult AJAX_DeleteUser(int userId)
		{
			var result = _uService.Delete(userId);
			return Json(result);
		}

		public ActionResult AJAX_PermitUser(int userId)
		{
			var user = _uService.GetById(userId);
			user.IsValid = true;
			var result = _uService.Save(user);
			return Json(result);
		}

		/// <summary>
		/// 删除一个待审核的用户
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public RedirectToRouteResult DeleteNotValidUser(string userId)
		{
			int intUserId;
			Result result = new Result(false);
			if (int.TryParse(userId, out intUserId))
			{
				result = _uService.Delete(intUserId);
			}
			return RedirectToAction(AD.Singleton.Authority.WaitForValidUserList);
		}

		/// <summary>
		/// 允许一个未验证用户
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public RedirectToRouteResult AllowNotValidUser(string userId)
		{
			int intUserId;
			Result result = new Result(false);
			if (int.TryParse(userId, out intUserId))
				result = _uService.AllowNotValidUser(intUserId);
			return RedirectToAction(AD.Singleton.Authority.WaitForValidUserList);
			//return RedirectToAction<AuthorityController>(c => c.WaitForValidUserList(null));
		}
	}

	public class AuthorityControllerActions : ControllerDefinition<AuthorityController>
	{
		public readonly string UserList = "UserList";
		public readonly string AssignAuthority = "AssignAuthority";
		public readonly string StoragerWhetherViewPurchaseOrder = "StoragerWhetherViewPurchaseOrder";
		public readonly string WaitForValidUserList = "WaitForValidUserList";
		public readonly string AllowNotValidUser = "AllowNotValidUser";
	}
}
